<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Nette\Mail\Message;
use Nette\Mail\SmtpMailer;

class UserController extends Controller
{
    /**
     * 获取用户收藏车辆的数据
     */
    public function  getCollectId(Request $req)
    {

        $car_id = $req->car_id;
        $uid = $req->user()->uid;
        $data = DB::table('collect')->where(['car_id'=>$car_id,'uid'=>$uid])->first();
        if ($data) {
            return 0;
        } else {
            $row = DB::table('collect')->insert([
                    'car_id'   => $req->car_id,
                    'uid'      => $req->user()->uid,
                    'car_city' => $req->city
                ]);
            return 1;
        }
    }

   /**
    * 获取用户取消收藏的数据
    */
    public function deleteCollect(Request $req)
    {
        $uid = $req->uid;
        $car_id = $req->car_id;
        $coll = DB::table('collect')->where(['uid'=>$uid,'car_id'=>$car_id])->delete();
        if ( $coll == 1 ) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 我的收藏
     */
    public function collectShow(Request $req)
    {
        $city = $req->city;
        // dd($city);
        /*获取用户ID*/
        $uid = $req->user()->uid;
        // dd($uid);
        /*获取car_id*/
        $collect = DB::table('collect')
                 ->where('uid','=',$uid)
                 ->select('car_id','car_city')
                 ->get();
        $citys = config('city');
        $username = $req->user()->username;
        $mobile = $req->user()->mobile;
        if ( $collect == [] ) {
            return view('collect',['citys'=>$citys,'username'=>$username,'mobile'=>$mobile,'city'=>$city]);
        } else {
            $car_id = [];
            foreach($collect as $k => $v){
                $car_id[] = $v->car_id;
            }
            // dd($car_id);
            $num = count($car_id);
            for($i=0;$i<$num;$i++){
                $collection[] = DB::table('car')
                        ->where('car.car_id','=',$car_id[$i])
                        ->join('survey','survey.car_id','=','car.car_id')
                        ->join('collect','collect.car_id','=','car.car_id')
                        ->select('car.brand_name','car.series_name','car.year_type','car.emission','car.type','car.reg_time','car.mileage','car.price','car.car_id','survey.index_img_path','collect.car_city')
                        ->paginate(1);
            }
            //dd($collection);
            return view('collect',['collection'=>$collection,'citys'=>$citys,'username'=>$username,'mobile'=>$mobile,'uid'=>$uid,'city'=>$city]);
        }

    }

    /**
     * 卖家砍价消息
     */
     public function sellerBargain(Request $req,$city)
     {
         /*获取城市*/
         $city = $req->city;
         /*根据获取到的uid*/
         $uid = $req->user()->uid;
         /*获取username*/
         $user = DB::table('users')->where(['uid'=>$uid])->select('username','mobile')->first();
         $username = $user->username;
         $mobile = $user->mobile;
         $citys = config('city');
        //  dd($username);
         $cars = DB::table('car')->where(['uid'=>$uid])->select('uid')->get();
         if($cars == []){
             $content = '亲,你当前还没有车辆在平台销售';
             return view('bargain',['content'=>$content,'city'=>$city,'citys'=>$citys,'username'=>$username,'mobile'=>$mobile]);
         } else {
            // dd($cars);
             foreach($cars as $k=>$v){
                 $cars = DB::table('bargain')
                    ->where(['sell_id'=>$v->uid,'status'=>0])
                    ->join('users','bargain.uid','=','users.uid')
                    ->select('bargain.cut_money','bargain.orig_price','bargain.car_id','bargain.bar_id','users.username','users.uid')
                    ->get();
             }
            //dd($cars);
             return view('bargain',['city'=>$city,'citys'=>$citys,'username'=>$username,'mobile'=>$mobile,'cars'=>$cars,'uid'=>$uid]);

         }
     }

     /**
      * 调用云片APi发送短信     由于云片标签的原因现在做不了
      */
      public function yunpian(Request $req)
      {
                // return $req->car_id;
                $uid = $req->uid;
                $car_id = $req->car_id;
                // $car_id = 3;
                $car = DB::table('car')->where(['car_id'=>$car_id])->select('brand_name','series_name')->first();
                $brand_name = $car->brand_name;
                $series_name = $car->series_name;
                $user = DB::table('users')->where(['uid'=>$uid])->select('email')->first();
                $email = $user->email;
                $mail = new Message;
                $mail->setFrom('旺崽 <1003230257@qq.com>')
                     ->addTo('1659349624@qq.com')
                     ->setSubject('瓜子二手车')
                     ->setBody("尊敬的用户:很遗憾!你对".$brand_name.$series_name."提交的砍价被卖家拒绝,感谢你对瓜子二手车的支持");
                $mailer = new SmtpMailer(
                       array(
                           'host' => 'smtp.qq.com',
                           'username' => '1003230257@qq.com',
                           'password' => 'awvmandhdzsebfdi',
                           'secure' => 'ssl'
                       )
                   );
                $mailer->send($mail);

                // /*以下为云片的核心代码*/
                // $ch = curl_init();
                // // 必要参数
                // $apikey = "7468adf213b75bdab3f4547655dde87a"; //修改为您的apikey(https://www.yunpian.com)登录官网后获取
                // $mobile = $mobile; //请用手机号代替
                // $text="【瓜子二手车】亲";
                // // 发送短信
                // $data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
                // curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
                // curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
                // curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
                // $json_data = curl_exec($ch);
                // //解析返回结果（json格式字符串）
                // $array = json_decode($json_data,true);
                // echo '<pre>';print_r($array);
      }
}
